/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sudoku;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;

/**
 *
 * @author Ronja
 */

public class Feld extends JButton implements Cloneable {
    ArrayList<String> numbers = new ArrayList();
    int entry;
    private int x;
    private int y;
    
   Feld(int i, int j){
       for (int a=1; a<10; a++){
           numbers.add(String.valueOf(a));
       }
       entry = 0;
       this.x=i;
       this.y=j;
   }
   
   public void reset(){
       this.setFont(new Font("sansserif", Font.PLAIN, 16));
       entry = 0;
       numbers.clear();
       for (int i=1; i<10; i++){
           numbers.add(String.valueOf(i));
       }
   }
   
   public void repainttext(){
       this.setText(this.entry==0? this.toString() : String.valueOf(this.entry));
       gui.freieFelder.setText("Free Fields: " + Main.free_fields);
    }
   
   void delete (String num){
       this.numbers.remove(num);
   }
   
   boolean enterNumber(String num){
       if (this.numbers.contains(num)){
           System.out.println(num + " bei " + (x+1) + ":" + (y+1));
           entry = Integer.parseInt(num);
           this.setFont(new Font("sansserif", Font.BOLD, 32));
           gui.back.setEnabled(true);
           this.numbers.clear();
           Main.free_fields--;
           Main.recalculate(x, y, num);
           Main.repaintall();
           return true;
       }
       else{
           System.out.println("Fehler: falsche Eingabe " + num + " bei " + (x+1) + ":" + (y+1));
           return false;
       }
   }
    @Override
    public String toString(){
        if (!gui.poss) return "";
        String s = "";
        for (String st : this.numbers){
            s = s + st;
        }
        return s;
   }
      
   void action(){
       this.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent evt) {
               
               //Zahleneingabe über ein OptionPanel
               String[] buttons = {"Cancel","1","2","3","4","5","6","7","8","9"};
               String num = String.valueOf(JOptionPane.showOptionDialog(null, 
               "Welche Zahl möchten Sie einfügen ?", "Zahleneingabe",
               JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
               null, buttons, buttons[0]));
               
               Main.save();
               System.out.print("manuelle Eingabe: ");
               enterNumber(num);
           }
       });
   }
}
